package com.drondea.testclient.handler.smgp;

import cn.hutool.core.util.StrUtil;
import com.drondea.sms.message.smgp30.msg.SmgpDeliverRequestMessage;
import com.drondea.sms.message.smgp30.msg.SmgpDeliverResponseMessage;
import com.drondea.testclient.panel.SmgpPanel;
import com.drondea.testclient.util.counter.SmgpAtomicUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @version V3.0.0
 * @description 回执处理器
 * @author 刘彦宁
 * @date 2020年06月22日08:48
 **/

@Slf4j
public class SmgpDeliveryRequestHandler extends SimpleChannelInboundHandler<SmgpDeliverRequestMessage> {

    private static final SmgpPanel PANEL = SmgpPanel.getInstance();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SmgpDeliverRequestMessage msg) {

        //判断是否是回执
        boolean report = msg.isReport();
        if (report) {
            PANEL.reportCountLabel.setText(StrUtil.format("报告数量: {}", SmgpAtomicUtil.REPORT_COUNT.incrementAndGet()));
        } else if (msg.isMsgComplete()) {
            //是否长短信组装完毕
            log.debug("组装完毕，短信内容：{}", msg.getMsgContent());
        }

        //发送响应
        SmgpDeliverResponseMessage deliverResponseMessage = new SmgpDeliverResponseMessage(msg.getHeader());
        deliverResponseMessage.setSmgpMsgId(msg.getSmgpMsgId());
        deliverResponseMessage.setStatus(0);
        ctx.channel().writeAndFlush(deliverResponseMessage);
    }
}
